package progetto;

import peersim.config.Configuration;
import peersim.core.Control;
import peersim.core.Network;
import peersim.core.Node;
import progetto.LocalView;

public class ManageLocalView implements Control{

	/**
     * The coordinate protocol to look at.
     * 
     * @config
     */
    private static final String PAR_COORDINATES_PROT = "coord_protocol";
    
    /**
     * @config
     */
    private static final String PAR_LV_PROT = "lv_protocol";
    
    //private static final String PAR_BT_PROT = "bt_protocol";
    
    
    
    private static int coordPid;
    
    private static int lvPid;
    
    //private static int btPid;
	
    
    public ManageLocalView(String prefix) {
    	coordPid = Configuration.getPid(prefix + "." + PAR_COORDINATES_PROT);
    	lvPid = Configuration.getPid(prefix + "." + PAR_LV_PROT);
    	//btPid = Configuration.getPid(prefix + "." + PAR_BT_PROT);
    }
    
	@Override
	public boolean execute() {
		// TODO Auto-generated method stub
		for(int i=0; i<Network.size(); i++) {
			Node node = Network.get(i);
			//devo rimuovere i suoi vicini
			LocalView lv = (LocalView)(node.getProtocol(lvPid));
			lv.cleanNeighbors();
			//devo prendere i suoi nuovi vicini
			BluetoothTransm bt = BluetoothTransm.getInstance();
			//DynamicCoordinates dc = (DynamicCoordinates)(node.getProtocol(coordPid));
			for(int j=0; j<Network.size(); j++) {
				Node current = Network.get(j);
				if(!node.equals(current) && bt.inTheTrasmRadius(node, current, coordPid)) {
					LocalView lvProt =  (LocalView)node.getProtocol(lvPid);
					lvProt.addNeighbor(current);
				}
			}
			
		}
		
		
		return false;
	}

}
